<?php

declare(strict_types=1);

/**
 * @Author:     Frank
 * @contact     frank_hhm@163.com
 * @Date:       2022-11-17 13:44:26
 * @Last Modified time: 2022-12-06 22:02:22
 */
namespace app\sys\controller\system;

use app\sys\controller\Base;
use think\facade\{
    App,
    Config
};
use app\common\services\system\AdminService;
use frank\services\CacheService;
use app\sys\validate\AdminValidate;
/**
 * 管理员
 * Class Admin 
 * @package app\sys\controller\system
 */
class Admin extends Base
{
    /**
     * Admin constructor.
     * @param App $app
     */
    public function __construct(App $app , AdminService $service)
    {
        parent::__construct($app);
        $this->service = $service;
    }

    /**
     * 管理员列表
     * @method(GET)
     */
    public function list()
    {
        $data = $this->request->getMore([
        ]);
        $this->success($this->service->getPageList($data));
    }

    /**
     * 添加管理员
     * @method(POST)
     */
    public function store()
    {
        $data = $this->request->postMore([
            ['account', ''],
            ['real_name', ''],
            ['avatar', ''],
            ['roles', []],
            ['pwd',''],
            ['conf_pwd','']
        ]);

        validate(AdminValidate::class)->check($data);

        $this->service->create($data);
        $this->success('添加管理员成功!');
    }

    /**
     * 修改管理员
     * @method(PUT)
     */
    public function update()
    {
        $data = $this->request->postMore([
            ['id', ''],
            ['account', ''],
            ['real_name', ''],
            ['avatar', ''],
            ['roles', []],
            ['pwd',''],
            ['conf_pwd','']
        ]);

        validate(AdminValidate::class)->check($data);

        if( $this->service->updateSave($this->request->param('id'),$data)){
            $this->success('修改管理员成功!');
        }
        $this->error('修改管理员失败!');
    }


    /**
     * 删除管理员
     * @method(DELETE)
     */
    public function delete()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        $admin = $this->service->getOne(['id' => $id]);
        if (!$admin) {
            $this->error('该管理员不存在！');
        }
        if (!$admin['level']) {
            $this->error('超级管理员不可删除！');
        }

        if ($this->service->destroy((int)$id)) {
            $this->success('删除成功!');
        } 
        $this->error('删除失败!');
    }

    /**
     * 修改管理员状态
     * @method(PUT)
     */
    public function status()
    {
        if (!$id = $this->request->param('id')) {
            $this->error('参数错误!');
        }
        if ($this->service->update($id, ['status' => $this->request->param('status')])) {
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }

    /**
     * 管理员详细 
     * @method(GET)
     */
    public function detail(){
        $this->success($this->service->getDetail($this->request->get('id')));
    }

     /**
     * 退出登陆
     * @noAuth(true)
     * @method(GET)
     */
    public function logout()
    {
        $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
        CacheService::instance()->redisHandler()->delete($key);
         $this->success('退出登陆成功');
    }
}